<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="hadd">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table :data="EmployeeList" border :default-sort="{prop: 'correctionTime', order: 'descending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />

          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ formOfEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>

          <el-table-column sortable label="入职时间" prop="correctionTime" />
          <!-- <el-table-column label="账户状态" prop="departmentName"/> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hdel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 4, 6, 8]"
            :page-size="size"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 新增组件 -->
      <el-dialog title="新增" :visible.sync="showDialog">
        <empDialog @close="showDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import empDialog from './empDialog.vue'
import { getEmployeeList, delEmployee } from '@/api/employees'
import employees from '@/constant/employees'
const hireType = {}
employees.hireType.map(item => {
  hireType[item.id] = item.value
})
// console.log(hireType)
export default {
  components: {
    empDialog
  },
  data() {
    return {
      showDialog: false,
      total: 0,
      EmployeeList: [],
      page: 1,
      size: 2
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    // 新增员工
    hadd() {
      this.showDialog = true
    },
    // 删除功能
    async hdel(id) {
      console.log(id)
      const res = delEmployee(id)
      console.log(res)
    },
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployeeList()
    },
    // 枚举
    formOfEmployment(id) {
      // console.log(id)
      return hireType[id]
    },
    // 员工列表
    async loadEmployeeList() {
      const res = await getEmployeeList({ page: this.page, size: this.size })
      // console.log(this.size)
      // console.log(res)
      this.EmployeeList = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
